package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import business.Aluno;
import business.Pesquisar;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.text.MaskFormatter;
import javax.swing.SwingUtilities;
import business.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Matricula extends javax.swing.JFrame {
	private JLabel jLblCPF;
	private JComboBox jCBoxCursos;
	private JButton jBtnCancelar;
	private JButton jBtnOK;
	private JLabel jLblCurso;
	private JTextField jTxtfdCPF;
    private String cpfAluno = null;
    private String idCurso;
    private String listaCursos[][];
    private Boolean alunoEncontrado = false;
	/**
	* Auto-generated main method to display this JFrame
	*/
	
		
	
	
	public Matricula() {
		super();
		Pesquisar pesquisar = new Pesquisar();
		listaCursos = pesquisar.buscarCursos();
		initGUI();
	}
	
	public Matricula(String cpf) {
		
		super();
		Pesquisar pesquisar = new Pesquisar();
		listaCursos = pesquisar.buscarCursos();
		cpfAluno = cpf;
		initGUI();
		
	}
	public Matricula(String cpfAluno2, String idCurso2) {
		// TODO Auto-generated constructor stub
	}

	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(192,192,192));
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("file/IconeCadastro2.png")).getImage());
			this.setTitle("Matricula");
			
			
			{
				jBtnCancelar = new JButton();
				getContentPane().add(jBtnCancelar, new AnchorConstraint(785, 766, 947, 633, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jBtnCancelar.setPreferredSize(new java.awt.Dimension(45, 36));
				jBtnCancelar.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				jBtnCancelar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("file/cancelar.jpg")));
				
				jBtnCancelar.addActionListener(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent arg0) {
						dispose();
						
					}
					
				
				});
				
			}
			{
				jBtnOK = new JButton();
				getContentPane().add(jBtnOK, new AnchorConstraint(785, 933, 947, 801, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jBtnOK.setPreferredSize(new java.awt.Dimension(45, 36));
				jBtnOK.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				jBtnOK.setIcon(new ImageIcon(getClass().getClassLoader().getResource("file/ok.jpg")));
				jBtnOK.setEnabled(false);
				jBtnOK.addActionListener(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent e) {
						
						cpfAluno = jTxtfdCPF.getText();
						idCurso = listaCursos[jCBoxCursos.getSelectedIndex()][1];
						business.Matricula objetoMatricula = new business.Matricula(cpfAluno, idCurso);
						objetoMatricula.inserirMatricula(objetoMatricula);
						dispose();
						JOptionPane.showMessageDialog(null, "Aluno Matriculado com Sucesso");
												
					}
					
					
				});
			}
			{
				ComboBoxModel jCBoxCursosModel = 
					new DefaultComboBoxModel(
							new String[] {});
				jCBoxCursos = new JComboBox();
				getContentPane().add(jCBoxCursos, new AnchorConstraint(233, 472, 337, 122, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jCBoxCursos.setModel(jCBoxCursosModel);
				jCBoxCursos.setPreferredSize(new java.awt.Dimension(119, 23));
				for(int i = 0; i < listaCursos.length;i++){
					jCBoxCursos.addItem(listaCursos[i][0]);
				}
			}
			{
				jLblCurso = new JLabel();
				getContentPane().add(jLblCurso, new AnchorConstraint(219, 122, 346, 13, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jLblCurso.setText("Curso:");
				jLblCurso.setPreferredSize(new java.awt.Dimension(37, 28));
			}
			{
				FocusListener listener = new FocusListener() {
					
					@Override
					public void focusLost(FocusEvent e) {
						Pesquisar objetoPesquisar = new Pesquisar();
						Aluno objetoAluno = null;
						objetoAluno = objetoPesquisar.pesquisarAluno(jTxtfdCPF.getText());
						jBtnOK.setEnabled(false);
						if (objetoAluno.getCPF().equals(jTxtfdCPF.getText())){
							jBtnOK.setEnabled(true);
							objetoAluno = null;
							
						}
						else{
							jBtnOK.setEnabled(false);
							objetoAluno = null;
						}
						
					}
					
					@Override
					public void focusGained(FocusEvent e) {
						// TODO Auto-generated method stub
						
					}
				};
				
				
				jTxtfdCPF = new JFormattedTextField(MascaraCPF("###.###.###-##"));
				getContentPane().add(jTxtfdCPF, new AnchorConstraint(47, 560, 133, 116, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jTxtfdCPF.setPreferredSize(new java.awt.Dimension(151, 19));
				jTxtfdCPF.setText(cpfAluno);
				
				jTxtfdCPF.addFocusListener(listener);
						
			
			
			}
			{
				jLblCPF = new JLabel();
				getContentPane().add(jLblCPF, new AnchorConstraint(47, 128, 131, 32, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jLblCPF.setText("CPF:");
				jLblCPF.setPreferredSize(new java.awt.Dimension(37, 22));
			}
			pack();
			this.setSize(356, 259);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	public MaskFormatter MascaraData(String Mascara){  
        
	       MaskFormatter F_Mascara = new MaskFormatter();  
	       try{  
	           F_Mascara.setMask(Mascara); //Atribui a mascara  
	           F_Mascara.setPlaceholderCharacter(' '); //Caracter para preencimento   
	       }  
	       catch (Exception excecao) {  
	       excecao.printStackTrace();  
	       }   
	       return F_Mascara;  
	}   
	
	public MaskFormatter MascaraCPF(String Mascara){  
     
	       MaskFormatter F_Mascara = new MaskFormatter();  
	       try{  
	           F_Mascara.setMask(Mascara); //Atribui a mascara  
	           F_Mascara.setPlaceholderCharacter(' '); //Caracter para preencimento   
	       }  
	       catch (Exception excecao) {  
	       excecao.printStackTrace();  
	       }   
	       return F_Mascara;  
	}   
}
